package com.googlecode.johmap.image;

import static java.lang.String.format;

import java.util.ArrayList;

import johmap.Tableimage;
import johmap.Tableimageplayer;
import johmap.Tableimagepot;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.googlecode.johmap.observer.TableImageObservable;
import com.googlecode.johmap.observer.TableImageObserver;

@Component
public class TableImageBo extends Tableimage implements TableImageObservable {

    private final ArrayList<TableImageObserver> observers = new ArrayList<TableImageObserver>();
    private static final Double defaultToZero = 0.0;
    protected final Logger logger = LoggerFactory.getLogger(getClass());

    public TableImageBo() {
    }


    public void setImage(Tableimage image) {
        if (image != null) {
            this.flopCardA = image.getFlopCardA();
            this.flopCardB = image.getFlopCardB();
            this.flopCardC = image.getFlopCardC();
            this.handNumber = image.getHandNumber();
            this.players = image.getPlayers();
            this.pots = image.getPots();
            this.riverCard = image.getRiverCard();
            this.snapshotTimestamp = image.getSnapshotTimestamp();
            this.tableTitle = image.getTableTitle();
            this.turnCard = image.getTurnCard();
            this.terrain = image.getTerrain();
        }
    }

    public void log() {

        // Further additions can be made to the logging here.

        logger.info(format("Logging %s number %s with %s players:", tableTitle, handNumber, players));
        if (flopCardA!=null) {
            logger.info(format("flopA: %d:%d", flopCardA.getRank(), flopCardA.getSuit()));
        }
        if (flopCardB != null) {
            logger.info(format("flopB: %d:%d", flopCardB.getRank(), flopCardB.getSuit()));
        }
        if (flopCardC != null) {
            logger.info(format("flopC: %d:%d", flopCardC.getRank(), flopCardC.getSuit()));
        }
        if (turnCard != null) {
            logger.info(format("turn : %d:%d", turnCard.getRank(), turnCard.getSuit()));
        }
        if (riverCard != null) {
            logger.info(format("river: %d:%d", riverCard.getRank(), riverCard.getSuit()));
        }
        if (terrain != null) {
            Double bb = terrain.getBigBlind();
            Double sb = terrain.getSmallBlind();
            if (bb == null) {
                bb = defaultToZero;
            }
            if (sb == null) {
                sb = defaultToZero;
            }
            logger.info(format("Terrain: BB=%s SB=%s", bb.toString(), sb.toString()));
        }

        if (players != null) {
            for (Tableimageplayer player : players) {
                Double balance = player.getBalance();
                if (balance == null) {
                    balance = defaultToZero;
                }
                logger.info(format("Player %s has %s chips", player.getName(), balance));
            }
        }

        if (pots != null) {
            for (Tableimagepot pot : pots) {
                Double total = pot.getTotal();
                if (total == null) {
                    total = defaultToZero;
                }
                logger.info(format("Pot: %s", total.toString()));
            }
        }

    }

    @Override
    // TODO - use spring to wire this in
    public void registerObserver(TableImageObserver observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(TableImageObserver observer) {
        observers.remove(observer);
    }

    @Override
    public void notifyObservers() {
        for (TableImageObserver obsever : observers) {
            logger.info(format("Observing change for table image."));
            obsever.observeChange(this);
        }

    }

}
